package com.hy.controller;

import com.hy.common.Result;
import com.hy.entity.Banner;
import com.hy.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/admin/banners")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    // 将 uploadPath 定义为类成员变量，@Value 才能正确注入
    @Value("${upload.banner-path}")
    private String uploadPath;

    /**
     * 轮播图上传
     * @param file
     * @return
     */
    @PostMapping("/uploadBannerImg")
    public Result<String> uploadTeacherAvatar(@RequestParam("file") MultipartFile file) {
        try {
            String originalFilename = file.getOriginalFilename();
            if (originalFilename == null || originalFilename.isEmpty()) {
                return Result.error("文件名为空");
            }
            String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFileName = UUID.randomUUID().toString() + fileSuffix;

            File destDir = new File(uploadPath);
            if (!destDir.exists()) {
                destDir.mkdirs(); // 创建目录
            }
            File destFile = new File(uploadPath + newFileName);
            file.transferTo(destFile); // 保存文件

            String accessUrl = "/banners/" + newFileName;
            return Result.success(accessUrl); // 返回可访问路径
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("上传失败：" + e.getMessage());
        }
    }


    /**
     * 获取轮播图列表
     * @return
     */
    @RequestMapping("/Bannerlist")
    public Result<List<Banner>> getAllBanners() {
        return Result.success(bannerService.list());
    }

    /**
     * 根据id获取Banner
     * @param id
     * @return
     */
    @GetMapping("/getBuBannerid")
    public Result<Banner> getBanner(@RequestParam("id") Long id) {
        return Result.success(bannerService.getById(id));
    }

    /**
     * 添加轮播图
     * @param banner
     * @return
     */
    @PostMapping("/addBanner")
    public Result<Boolean> addBanner(@RequestBody Banner banner) {
        try {
            bannerService.save(banner);
            return Result.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("修改失败");
    }

    /**
     * 修改轮播图
     * @param banner
     * @return
     */
    @GetMapping("/updateBanner")
    public Result<Boolean> updateBanner(@RequestBody Banner banner) {
        try {
            bannerService.updateById(banner);
            return Result.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("修改失败");
    }

    /**
     * 删除轮播图
     * @param id
     * @return
     */
    @DeleteMapping("/deleteBanner")
    public Result<Boolean> deleteBanner(@RequestParam("id") Long id) {
        try {
            bannerService.removeById(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("删除失败");
    }
} 